package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatContentProtocol;
import com.happy.cat.project.system.service.ICatContentProtocolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 协议管理Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/contentProtocol" )
@Api("协议管理 API")
public class CatContentProtocolController extends BaseController {

    private final ICatContentProtocolService iCatContentProtocolService;

    /**
     * 查询协议管理列表
     */
    @ApiOperation(value = "查询协议管理列表",notes = "查询协议管理列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:contentProtocol:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatContentProtocol catContentProtocol)
    {
        startPage();
        LambdaQueryWrapper<CatContentProtocol> lqw = new LambdaQueryWrapper<CatContentProtocol>();
        if (StringUtils.isNotBlank(catContentProtocol.getName())){
            lqw.like(CatContentProtocol::getName ,catContentProtocol.getName());
        }
        if (StringUtils.isNotBlank(catContentProtocol.getLink())){
            lqw.eq(CatContentProtocol::getLink ,catContentProtocol.getLink());
        }
        if (StringUtils.isNotBlank(catContentProtocol.getContent())){
            lqw.eq(CatContentProtocol::getContent ,catContentProtocol.getContent());
        }
        if (catContentProtocol.getCreateDate() != null){
            lqw.eq(CatContentProtocol::getCreateDate ,catContentProtocol.getCreateDate());
        }
        if (catContentProtocol.getStatus() != null){
            lqw.eq(CatContentProtocol::getStatus ,catContentProtocol.getStatus());
        }
        List<CatContentProtocol> list = iCatContentProtocolService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出协议管理列表
     */
    @ApiOperation(value = "导出协议管理列表",notes = "导出协议管理列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:contentProtocol:export')" )
    @Log(title = "协议管理" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatContentProtocol catContentProtocol) {
        LambdaQueryWrapper<CatContentProtocol> lqw = new LambdaQueryWrapper<CatContentProtocol>(catContentProtocol);
        List<CatContentProtocol> list = iCatContentProtocolService.list(lqw);
        ExcelUtil<CatContentProtocol> util = new ExcelUtil<CatContentProtocol>(CatContentProtocol. class);
        return util.exportExcel(list, "contentProtocol" );
    }

    /**
     * 获取协议管理详细信息
     */
    @ApiOperation(value = "获取协议管理详细信息",notes = "获取协议管理详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:contentProtocol:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatContentProtocolService.getById(id));
    }

    /**
     * 新增协议管理
     */
    @ApiOperation(value = "新增协议管理",notes = "新增协议管理",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:contentProtocol:add')" )
    @Log(title = "协议管理" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatContentProtocol catContentProtocol) {
        catContentProtocol.setCreateDate(new Date());
        catContentProtocol.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatContentProtocolService.save(catContentProtocol) ? 1 : 0);
    }

    /**
     * 修改协议管理
     */
    @ApiOperation(value = "修改协议管理",notes = "修改协议管理",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:contentProtocol:edit')" )
    @Log(title = "协议管理" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatContentProtocol catContentProtocol) {
        catContentProtocol.setCreateDate(new Date());
        catContentProtocol.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatContentProtocolService.updateById(catContentProtocol) ? 1 : 0);
    }

    /**
     * 删除协议管理
     */
    @ApiOperation(value = "删除协议管理",notes = "删除协议管理",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:contentProtocol:remove')" )
    @Log(title = "协议管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatContentProtocolService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
